package show.lmm.spring_data_r2dbc.core.conf;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import show.lmm.spring_data_r2dbc.core.DynamicDataSource;

@Configuration
public class DataSourceConf {

    @Bean(name = "dynamicDataSource")
    @Qualifier("dynamicDataSource")
    public DynamicDataSource dynamicDataSource(AppConf appConf) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        appConf.getDataSources().forEach(dynamicDataSource::addDataSource);
        dynamicDataSource.setTargetConnectionFactories();
        dynamicDataSource.setDefaultTargetConnectionFactory(dynamicDataSource.getFirstConnection());
        return dynamicDataSource;
    }
}
